import $http from '../../../servers/http';
import { Result } from 'seats/schema/Result';

import { encryptStr } from 'utils/tools';
import { LoginResponse, UserInfo } from 'seats/schema/Login';
import { FetchPermissionResponse } from 'seats/schema/PermissionTypes';
/* 获取验证码
---------------------------------------------------------------- */
export const getVerifyCode = (data: { captchaId: string }): Promise<any> => {
  return $http.get({
    url: `/sso/captcha`,
    data,
  });
};
/* 登录
---------------------------------------------------------------- */
export const login = (data: {
  account: string;
  pwd: string;
  captcha: string;
  captchaId: string;
}): Promise<Result<LoginResponse>> => {
  return $http.post({
    url: `/seats/login`,
    data,
  });
};
/* 退出
---------------------------------------------------------------- */
export const logout = (): Promise<Result<LoginResponse>> => {
  return $http.put({
    url: `/seats/logout`,
  });
};
/* 右上角-设置弹窗----------------------------------------------------- */
/**个人资料 */
export const getSysUserInfo = (): Promise<Result<UserInfo>> => {
  return $http.get({
    url: `/sso/user/userinfo`,
  });
};

/**修改密码 */
export const modifySysUserPwd = (old: string, newPwd: string): Promise<Result<null>> => {
  return $http.patch({
    url: '/sso/user/modifypassword',
    data: {
      oldPassword: encryptStr(old),
      newPassword: encryptStr(newPwd),
    },
  });
};

/**查询菜单功能权限 */
export const getMenuPermission = (): Promise<FetchPermissionResponse> => {
  return $http.get({
    url: `/sso/menu`,
  });
};
/**查询角色功能权限 */
export const getRolePermission = (roleId: number): Promise<FetchPermissionResponse> => {
  return $http.get({
    url: `/sso/role/permissions/${roleId}`,
  });
};
